<template>
  <div class="addAdvertise">
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="广告名称" prop="name">
          <el-input v-model="form.name" size="small"></el-input>
        </el-form-item>
        <el-form-item label="广告位置" prop="spaceId">
          <el-select v-model="form.spaceId" placeholder="请选择活动区域" size="small">
            <el-option
              v-for="item in adSpace"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="date"
            size="small"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            size="small"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="form.status" size="small">
            <el-radio :label="1">上线</el-radio>
            <el-radio :label="0">下线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="/front/upload/img"
            :on-success="handleSuccess">
            <el-button size="small" type="primary">选择图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="广告链接" prop="link">
          <el-input v-model="form.link" size="small"></el-input>
        </el-form-item>
        <el-form-item label="广告备注" prop="text">
          <el-input type="textarea" v-model="form.text" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" size="small">提交</el-button>
          <el-button size="small" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getAllSpaces, getAdsSaveOrUpdate, getAdById } from '@/services/ad'
export default {
  name: 'addAdvertise',
  data () {
    return {
      form: {
        status: 0
      },
      adSpace: [],
      // 指定表单规则
      rules: {
        name: [
          { required: true, message: '请填写广告名称', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请填写开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请填写结束时间', trigger: 'change' }
        ],
        link: [
          { required: true, message: '请填写广告链接', trigger: 'blur' }
        ]
      },
      // 接收从list组件传过来的id值
      id: ''
    }
  },
  created () {
    this.getAllSpaces()
    this.id = this.$route.params.id
    if (this.id) {
      this.getAdById()
    }
  },
  methods: {
    // 获取广告位置
    async getAllSpaces () {
      const { data } = await getAllSpaces()
      console.log(data)
      if (data.state === 1) {
        this.adSpace = data.content
      }
    },
    // 存储照片
    handleSuccess (response) {
      this.form.img = response.file
    },
    // 提交表单数据
    async onSubmit () {
      await this.$refs.form.validate()
      const { data } = await getAdsSaveOrUpdate(this.form)
      if (data.state === 1) {
        this.$message.success('提交成功')
        this.$router.push({ name: 'advertise' })
      }
    },
    // 重置表格
    resetForm () {
      this.$refs.form.resetFields()
    },
    // 通过id 获取广告信息
    async getAdById () {
      const { data } = await getAdById(this.id)
      console.log(data)
      if (data.state === 1) {
        this.form = data.content
      }
    },
    handleAvatarSuccess (file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dotted
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
